// ProntoIDGrabber.cpp: implementation of the CProntoIDGrabber class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "ProntoHelper.h"
#include "ProntoIDGrabber.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CProntoIDGrabber::CProntoIDGrabber()
{

}

CProntoIDGrabber::~CProntoIDGrabber()
{

}


/////////////////////////////////////////////////////////////////
// ParseProntoID()
// Parse pronto ID in string or short paragraph
// 2010.11.22 jun-harry.he@nsn.com
//
// pronto ID rule:
// 1. internal pronto, length = 11 and end with ESPExx
// 2. customer pronto, length = 10 and intial with NA
//
// return rule:
// 1. if more than 1 pronto id included in strSentence, only first pronto id will reutrn
// 2. if no pronto id is detected, empty string is returned
//
CString CProntoIDGrabber::ParseProntoID(CString strSentence)
{
    int iPos = 0;
    int iCursor = 0;
    char szProntoID[16] = {0};

    // scan string to figure out all word(only alpha and digtal are accepted for a legal word)
    // all other characts are processed as separator
    for(;iPos<strSentence.GetLength();iPos++)
    {
        if(isdigit(strSentence[iPos]) || isalpha(strSentence[iPos]))
        {
            szProntoID[iCursor++] = strSentence[iPos];
        }
        else
        {
            // process internal pronto ID
            if((11 == iCursor) && IsInternalProntoID(CString(szProntoID)))
                break;

            // process custom pronto
            else if((10 == iCursor) && IsCustomerProntoID(CString(szProntoID)))
                break;

            // not a legal pronto id,reset buff
            iCursor = 0;
            memset(szProntoID,0,16);
            continue;
        }            
    }

    // process internal pronto ID
    if((11 == iCursor) && IsInternalProntoID(CString(szProntoID)))
        return CString(szProntoID);

    // process custom pronto
    else if((10 == iCursor) && IsCustomerProntoID(CString(szProntoID)))
        return CString(szProntoID);

    memset(szProntoID,0,16);

    return CString(szProntoID);
}


// example:	65791ESPE02
BOOL CProntoIDGrabber::IsInternalProntoID(CString prontoID)
{
    return (0 == prontoID.Mid(5,4).CompareNoCase("ESPE"));
}

// example: NA04813398
BOOL CProntoIDGrabber::IsCustomerProntoID(CString prontoID)
{
    return (0 == prontoID.Left(2).CompareNoCase("NA"));
}